package config

import (
	"fmt"
	"github.com/spf13/viper"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"io"
	"os"
	"path/filepath"
)

func GetEncoder() zapcore.Encoder {
	return zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig())
}

func GetLogWriter() zapcore.WriteSyncer {
	dir, err := os.Getwd()
	if err != nil {
		panic(fmt.Errorf("获取工作路径失败 err=%v", err))
	}
	logDir := viper.GetString("log.dir")
	err = os.MkdirAll(logDir, 0755)
	if err != nil {
		panic(err)
	}
	LogFile := filepath.Join(dir, logDir, viper.GetString("log.filename"))
	//fmt.Printf("logFile=%v\n", LogFile)
	file, err := os.OpenFile(LogFile, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0644)
	if err != nil {
		panic(fmt.Errorf("创建日志文件失败err=%v", err))
	}
	ws := io.MultiWriter(file, os.Stdout)
	return zapcore.AddSync(ws)
}
